package io.gitee.xyuesheng.day02;

import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;
import java.util.Scanner;

public class HomeworkTaskArray {
    //数组是否已排序
    @Test
//输入一个数组
//    public static void main(String[] args){
//        Scanner input = new Scanner(System.in);
//        String[] str = null;
//        str = input.nextLine().split(" ");
//        int[] array= new int[str.length];
//        for (int i = 0 ;i < str.length; i ++) {
//            array[i] = Integer.valueOf(str[i]);
//        }
//        if (isSorted(array)){
//            System.out.println("已排序");
//        }else {
//            System.out.println("未排序");
//        }
//    }
    public void testSorted() {
        int[] array1 = {2, 8, 10, 12, 33};
        int[] array2 = {2, 4, 3, 9, 10};
        Assert.assertEquals(true, isSorted(array1));
        Assert.assertEquals(false, isSorted(array2));
    }

    public static boolean isSorted(int[] numbers) {
        if (numbers[0] <= numbers[numbers.length - 1]) {
            for (int i = 0; i < numbers.length - 1; i++) {
                //从小到大排列
                if (numbers[i] > numbers[i + 1]) {
                    return false;
                }
            }
            return true;
        } else {
            //从大到小排列
            for (int i = 0; i < numbers.length - 1; i++) {
                if (numbers[i] < numbers[i + 1]) {
                    return false;
                }
            }
            return true;
        }
    }

    //数组倒置
    @Test
    public void testReverse() {
        int[] array = {2, 8, 10, 12, 33};
        int[] expected = {33, 12, 10, 8, 2};
        Assert.assertArrayEquals(expected, reverse(array));
    }

    public int[] reverse(int[] array) {
        int[] result = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[array.length - i - 1];
        }
        return result;
    }

    //数组打乱顺序
    @Test
    public void testShuffle() {
        int[] array = {2, 8, 10, 12, 33};
        shuffle(array);
    }

    public static void shuffle(int[] numbers) {
        for (int i = 0; i < numbers.length; i++) {
            int j = (int) (Math.random() * numbers.length);
            int result = numbers[i];
            numbers[i] = numbers[j];
            numbers[j] = result;
        }
        System.out.println(Arrays.toString(numbers));
    }

    //连续相等的数判断
    @Test
    public void testBoolean() {
        int[] array1 = {8, 9, 9, 9, 9, 8};
        int[] array2 = {8, 9, 9, 9, 8};
        int[] array3 = {1, 2, 3, 3, 3, 3, 2,};
        hasContinuous(array1);
        hasContinuous(array2);
        hasContinuous(array3);
    }

    public static boolean hasContinuous(int[] numbers) {
        for (int i = 0; i < numbers.length - 3; i++) {
            int num = 1;
            for (int j = i + 1; j < i + 4; j++) {
                if (numbers[i] != numbers[j]) {
                    break;
                } else {
                    num++;
                    if (num == 4) {
                        System.out.println("正确");
                    }
                }
            }
        }
        return false;
    }

    @Test
    public void testScores() {
        int[][] scores = {
                {11, 12, 13, 14},
                {21, 22, 23, 24},
                {31, 32, 33, 34}
        };
//        for(int i=0;i<3;i++){
//            for(int j=0;j<4;j++){
//                System.out.print(scores[i][j]+" ");
//            }
//            System.out.println();
//        }

//        for(int i=2;i>=0;i--){
//            for(int j=0;j<4;j++){
//                System.out.print(scores[i][j]+" ");
//            }
//            System.out.println(" ");
//        }

//        for(int i=2;i>=0;i--){
//            for(int j=3;j>=0;j--){
//                System.out.print(scores[i][j]+" ");
//            }
//            System.out.println(" ");
//        }

        //顺时针
//        for (int i = 0; i < 4; i++) {
//            for (int j = 2; j >= 0; j--) {
//                System.out.print(scores[j][i] + " ");
//            }
//            System.out.println(" ");
//        }

        //逆时针
        for (int i = 3; i >= 0; i--) {
            for (int j = 0; j <= 2; j++) {
                System.out.print(scores[j][i] + " ");
            }
            System.out.println(" ");
        }


    }


}
